﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:TestTubeInfo
	/// </summary>
	public partial class TestTubeInfo
	{
		public TestTubeInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long testTubeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TestTubeInfo");
			strSql.Append(" where testTubeID=@testTubeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeID", DbType.Int64,8)			};
			parameters[0].Value = testTubeID;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.TestTubeInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TestTubeInfo(");
			strSql.Append("SampleID,TestTubeState,TestTubeSelfID,holeNumber,note)");
			strSql.Append(" values (");
			strSql.Append("@SampleID,@TestTubeState,@TestTubeSelfID,@holeNumber,@note)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@SampleID", DbType.Int64,8),
					new SQLiteParameter("@TestTubeState", DbType.Byte,1),
					new SQLiteParameter("@TestTubeSelfID", DbType.Int32,4),
					new SQLiteParameter("@holeNumber", DbType.Byte,1),
					new SQLiteParameter("@note", DbType.String,255)};
			parameters[0].Value = model.SampleID;
			parameters[1].Value = model.TestTubeState;
			parameters[2].Value = model.TestTubeSelfID;
			parameters[3].Value = model.holeNumber;
			parameters[4].Value = model.note;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.TestTubeInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TestTubeInfo set ");
			strSql.Append("SampleID=@SampleID,");
			strSql.Append("TestTubeState=@TestTubeState,");
			strSql.Append("TestTubeSelfID=@TestTubeSelfID,");
			strSql.Append("holeNumber=@holeNumber,");
			strSql.Append("note=@note");
			strSql.Append(" where testTubeID=@testTubeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@SampleID", DbType.Int64,8),
					new SQLiteParameter("@TestTubeState", DbType.Byte,1),
					new SQLiteParameter("@TestTubeSelfID", DbType.Int32,4),
					new SQLiteParameter("@holeNumber", DbType.Byte,1),
					new SQLiteParameter("@note", DbType.String,255),
					new SQLiteParameter("@testTubeID", DbType.Int64,8)};
			parameters[0].Value = model.SampleID;
			parameters[1].Value = model.TestTubeState;
			parameters[2].Value = model.TestTubeSelfID;
			parameters[3].Value = model.holeNumber;
			parameters[4].Value = model.note;
			parameters[5].Value = model.testTubeID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long testTubeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TestTubeInfo ");
			strSql.Append(" where testTubeID=@testTubeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeID", DbType.Int64,8)			};
			parameters[0].Value = testTubeID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string testTubeIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TestTubeInfo ");
			strSql.Append(" where testTubeID in ("+testTubeIDlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeInfo GetModel(long testTubeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select testTubeID,SampleID,TestTubeState,TestTubeSelfID,holeNumber,note from TestTubeInfo ");
			strSql.Append(" where testTubeID=@testTubeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeID", DbType.Int64,8)			};
			parameters[0].Value = testTubeID;

			Clipper.Model.TestTubeInfo model=new Clipper.Model.TestTubeInfo();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeInfo DataRowToModel(DataRow row)
		{
			Clipper.Model.TestTubeInfo model=new Clipper.Model.TestTubeInfo();
			if (row != null)
			{
				if(row["testTubeID"]!=null && row["testTubeID"].ToString()!="")
				{
					model.testTubeID=long.Parse(row["testTubeID"].ToString());
				}
				if(row["SampleID"]!=null && row["SampleID"].ToString()!="")
				{
					model.SampleID=long.Parse(row["SampleID"].ToString());
				}
				if(row["TestTubeState"]!=null && row["TestTubeState"].ToString()!="")
				{
					model.TestTubeState=int.Parse(row["TestTubeState"].ToString());
				}
				if(row["TestTubeSelfID"]!=null && row["TestTubeSelfID"].ToString()!="")
				{
					model.TestTubeSelfID=int.Parse(row["TestTubeSelfID"].ToString());
				}
				if(row["holeNumber"]!=null && row["holeNumber"].ToString()!="")
				{
					model.holeNumber=int.Parse(row["holeNumber"].ToString());
				}
				if(row["note"]!=null)
				{
					model.note=row["note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select testTubeID,SampleID,TestTubeState,TestTubeSelfID,holeNumber,note ");
			strSql.Append(" FROM TestTubeInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TestTubeInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.testTubeID desc");
			}
			strSql.Append(")AS Row, T.*  from TestTubeInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "TestTubeInfo";
			parameters[1].Value = "testTubeID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

